/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Example_01_Thread_Creation.java]
 * Creation Date        [02-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_08_thread;

/**
 * 创建线程
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_01_Thread_Creation {

	// 实现方式1 - 实现Runnable接口
	class ThreadImpl01 implements Runnable {
		@Override
		public void run() {
			// do something
			System.out.println("Thread 1");
		}
	}

	// 实现方式2 - 继承Thread类
	class ThreadImpl02 extends Thread {
		@Override
		public void run() {
			// do something else
			System.out.println("Thread 2");
		}
	}

	@SuppressWarnings("unused")
	private void dataShare() {
		Runnable r1 = new ThreadImpl01();
		Runnable r2 = new ThreadImpl01();
		// t1和t2共享代码和数据
		Thread t1 = new Thread(r1);
		Thread t2 = new Thread(r1);
		// t2和t3共享代码, 但不共享数据
		Thread t3 = new Thread(r2);
		// t4与其他线程既不共享代码, 也不共享数据
		Thread t4 = new ThreadImpl02();

	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		Example_01_Thread_Creation tc = new Example_01_Thread_Creation();

		Runnable r = tc.new ThreadImpl01();
		Thread t01 = new Thread(r);
		t01.start();

		Thread t02 = tc.new ThreadImpl02();
		t02.start();
	}

}
